/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cassandrawindow;
import java.util.zip.CRC32;
import java.util.zip.Checksum;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.*;
import java.text.*;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
/**
 *
 * @author quyennb
 */
public class TimeUtil {

    private static final SimpleDateFormat monthDayYearformatter = new SimpleDateFormat("dd/MMMMM/yyyy");

    private static final SimpleDateFormat monthDayformatter = new SimpleDateFormat("MMMMM dd");

    public static String convertTimeToString(long time)
    {
        String date = new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.util.Date(time*1000));
        //System.out.println(date.toString());
        return date.toString();
    }

    public static String getTimeInString(long time)
    {
        String date = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date(time));
        //System.out.println(date.toString());
        return date.toString();
    }

    public static String getTimeInStringForCronjob(long time)
    {
        String date = new java.text.SimpleDateFormat("yyyy/MM/dd").format(new java.util.Date(time));
        date = date.replace('/', '-');
        return date;
    }

    // Getting the current epoch time

    public static long getCurrentTime()
    {
        return (long) (System.currentTimeMillis() / 1000);
    }
    
    public static String getCurrentTimeInString()
    {
        long currTime = System.currentTimeMillis();
        String date = new java.text.SimpleDateFormat("yyyy/MM/dd").format(new java.util.Date(currTime));
        date = date.replace('/', '-');
        return date;
    }

    public static String getCurrentTimeInStringForLog()
    {
        long currTime = System.currentTimeMillis();
        String date = new java.text.SimpleDateFormat("yyyy/MM/dd H:mm:ss").format(new java.util.Date(currTime));
        date = date.replace('/', '-');
        return date;
    }

    public static String getCurrentTimeNoHour()
    {
        long currTime = System.currentTimeMillis();
        String date = new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.util.Date(currTime));
        //date = date.replace('/', '-');
        return date;
    }

    public static String getHour()
    {
        long currTime = System.currentTimeMillis();
        String hour = new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date(currTime));
        //date = date.replace('/', '-');
        return hour;
    }


    public static String getTimeInCurrent()
    {
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Calendar cal = Calendar.getInstance();
        return  dateFormat.format(cal.getTime());

    }

    public static String timestampToMonthDayYear(Timestamp timestamp) {
        if (timestamp == null)
        {
          return null;
        } else
        {
          return monthDayYearformatter.format((java.util.Date) timestamp);
        }
    }

    public static String timestampToMonthDay(Timestamp timestamp) {
        if (timestamp == null) {
          return null;
        } else {
          return monthDayformatter.format((java.util.Date) timestamp);
        }
    }

      public static java.sql.Timestamp getTimestamp() {
        java.util.Date today = new java.util.Date();
        return new java.sql.Timestamp(today.getTime());
      }

    public static long DateTimeToLong(Date date)
    {
        return date.getTime();
    }

    public static int getMonth()
    {
            Date date = new Date();   // given date
            Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
            calendar.setTime(date);   // assigns calendar to given date

            return calendar.get(Calendar.MONTH);
    }
     public static int getYear()
    {
            Date date = new Date();   // given date
            Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
            calendar.setTime(date);   // assigns calendar to given date

            return calendar.get(Calendar.YEAR);
    }

    public static int getHourLy()
    {
            Date date = new Date();   // given date
            Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
            calendar.setTime(date);   // assigns calendar to given date

            return calendar.get(Calendar.HOUR_OF_DAY);
    }

    public static long longToDate(String str) throws ParseException
    {
        SimpleDateFormat df = (SimpleDateFormat) SimpleDateFormat.getInstance();
        df.applyPattern("dd/MM/yyyy");
       // Date date = df.parse("11/2/2008 00:00:00");
        Date date = df.parse(str);
        //System.out.println(date);
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        //System.out.println(cal.getTimeInMillis()/1000);
        return cal.getTimeInMillis()/1000;
    }

    public static long longToDateWithHour(String str) throws ParseException
    {
        SimpleDateFormat df = (SimpleDateFormat) SimpleDateFormat.getInstance();
        df.applyPattern("HH:mm:ss dd-MM-yyyy");
        
       // Date date = df.parse("11/2/2008 00:00:00");
        Date date = df.parse(str);
        //System.out.println(date);
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        //System.out.println(cal.getTimeInMillis()/1000);
        return cal.getTimeInMillis();
    }

    public static long longToDateWithHourBanner(String str) throws ParseException
    {
        SimpleDateFormat df = (SimpleDateFormat) SimpleDateFormat.getInstance();
        df.applyPattern("HH:mm:ss yyyy-MM-dd");

       // Date date = df.parse("11/2/2008 00:00:00");
        Date date = df.parse(str);
        //System.out.println(date);
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        //System.out.println(cal.getTimeInMillis()/1000);
        return cal.getTimeInMillis();
    }


    public static long dateToUnixTime() throws ParseException
    {
        long epoch = new java.text.SimpleDateFormat ("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime();
        //long epoch = new java.text.SimpleDateFormat ("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime();

        return epoch;
    }

    public static int convertMonthStringToInteger(String day)
    {
        int value = 0;
        String arr[] = new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        for (int i = 0; i < arr.length; i++)
        {
            if(arr[i].contains(day))
            {
                value = i+1;
                break;
            }
        }
        return value;
    }

    public static long convertDatetoUnixTime(String hour, String day, String month) throws ParseException
    {
        long value = 0;
        int month_value = convertMonthStringToInteger(month);
        int year_value = TimeUtil.getYear();
        
        String date_value = hour +":00 " + day + "/" + String.valueOf(month_value)+"/"+String.valueOf(year_value);
        //Debug.println(date_value);
        value = TimeUtil.longToDateWithHour(date_value);
        return value;
    }

    public static boolean checkTime(int hour, int minute)
    {
            boolean check = false;

            Date date = new Date();   // given date
            Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
            calendar.setTime(date);   // assigns calendar to given date
            calendar.get(Calendar.HOUR_OF_DAY); // gets hour in 24h format
            calendar.get(calendar.MINUTE);
            calendar.get(calendar.SECOND);

            //if ((calendar.get(Calendar.HOUR_OF_DAY) == hour)&&(calendar.get(calendar.MINUTE) == minute))
            if ((calendar.get(Calendar.HOUR_OF_DAY) == hour))
            {
                check = true;
            }
            return check;
    }
    
     public static long cRC32Domain(String domain){
            //Convert string to bytes
            byte bytes[] = domain.getBytes();
            Checksum checksum = new CRC32();
            /*
             * To compute the CRC32 checksum for byte array, use
             *
             * void update(bytes[] b, int start, int length)
             * method of CRC32 class.
             */
            checksum.update(bytes,0,bytes.length);
            /*
             * Get the generated checksum using
             * getValue method of CRC32 class.
             */
            long lngChecksum = checksum.getValue();

            //System.out.println("CRC32 checksum for byte array is :" + lngChecksum);
            return lngChecksum;
        }


}
